package com.yonyou.openapi.event.todo;

import com.google.common.collect.Sets;
import com.yonyou.openapi.base.auth.network.cryptor.EncryptionHolder;
import com.yonyou.openapi.base.event.EventListener;
import com.yonyou.openapi.base.utils.JacksonUtils;
import com.yonyou.openapi.event.ISVEventExtendDto;
import com.yonyou.openapi.event.IsvEventExtendType;
import com.yonyou.openapi.service.SSOToYSIndexService;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Set;

/**
 * @description: 待办新增
 * @author: nishch
 * @create: 2021-07-05
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class TodoAddEvent implements EventListener<ISVEventExtendDto> {

    @Resource
    SSOToYSIndexService ssoToYSIndexService;

    /**
     * TODO:在这里写待办的处理逻辑
     * @param type 事项类型
     * @param event 解析后对象
     * @param holder 响应消息体
     * @return boolean
     */
    @Override
    @SneakyThrows
    public boolean onEvent(String type, ISVEventExtendDto event, EncryptionHolder holder){
        Map<String,String> info = JacksonUtils.toJavaObject(event.getContent(), Map.class);
        Map<String,Object> result = JacksonUtils.toJavaObject(info.get("value"), Map.class);
        log.info("todo id: {}", result.toString());
        log.info("todo webUrl: {}", URLEncoder.encode(result.get("webUrl").toString(), StandardCharsets.UTF_8.toString()));
        return true;
    }

    @Override
    public int priority() {
        return EventListener.super.priority()-100;
    }

    @Override
    public Set<String> supportTypes() {
        return Sets.newHashSet(IsvEventExtendType.TODO_CENTER_ADD_TODO);
    }

    @Override
    public Class<ISVEventExtendDto> getEventClass() {
        return ISVEventExtendDto.class;
    }

}
